package com.caxxb.controller;


import cn.hutool.crypto.SecureUtil;
import com.caxxb.common.R;
import com.caxxb.entity.GameMaster;
import com.caxxb.entity.User;
import com.caxxb.service.GameMasterService;
import com.caxxb.service.impl.GameMasterServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("gamemaster")
@CrossOrigin("*")
public class GameMasterController {

    private GameMasterService service = new GameMasterServiceImpl();


    //登录功能
    @PostMapping("login")
    public R<GameMaster> login(@RequestBody GameMaster gameMaster, HttpServletRequest request){
//        log.info(map.toString());
        //得到输入的账户密码
        String gm_name = gameMaster.getGm_name();
        //对密码进行加密处理
        String gm_password = gameMaster.getGm_password();
        String md5password = SecureUtil.md5(gm_password);

//        System.out.println("加密后密码"+md5password);

        List<GameMaster> login = service.login(gm_name,md5password);

        System.out.println(login);
        //登录失败返回错误信息
        if (login.size()==0){
            return R.error("登录失败");
        }

        //获取登录管理员对象
        GameMaster loginGameMaster = login.get(0);
        //得到管理员id
        int gm_id = loginGameMaster.getGm_id();

        //在session中存放用户id
        request.getSession().setAttribute("gameMaster",gm_id);
        System.out.println("管理员已登录");

//        String ip = getIpAddr(request);

//        LocalDateTime now = LocalDateTime.now();
//        String time = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

//        service.recordLog(userid,ip,now);
       /* System.out.println("ip:"+ip);
        System.out.println("userid:"+userid);
        System.out.println("time:"+time);*/

        return R.success(gameMaster);
    }

}
